﻿using System;
using SGC.CL;
using NHibernate;
using System.Collections;
using System.Collections.Generic;
using NHibernate.Criterion;
using NHibernate.Transform;

namespace SGC.DL.Comun
{
    public abstract class DL_NHRepositorio<T> : IRepositorio<T> where T : class
    {
        public int Registrar(T entidad)
        {
            int result = 0;
            try
            {
                ISession session = DL_NHibernate.GetSession().OpenSession();
                session.Save(entidad);
                session.Flush();
                return result;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.InnerException.Message);
                result = 1;
                return result;
            }
                  
        }

        public int Modificar(T entidad)
        {
            int result = 0;
            try
            {
                ISession session = DL_NHibernate.GetSession().OpenSession();
                session.Update(entidad);
                session.Flush();
                return result;
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                //Console.WriteLine(ex.InnerException.Message);
                result = 1;
                return result;
            }
                        
        }

        public int Eliminar(T entidad)
        {
            int result = 0;
            try
            {
                ISession session = DL_NHibernate.GetSession().OpenSession();
                session.Delete(entidad);
                session.Flush();
                return result;
            }
            catch (Exception)
            {
                result = 1;
                return result;
            }
            
        }

        public T ListarporId(string id)
        {
            try
            {
                ISession session = DL_NHibernate.GetSession().OpenSession();
                T entidad = (T)session.Get<T>(id);
                return entidad;
            }
            catch (Exception)
            {
                return null;
            }           
            
        }
        
        public System.Collections.Generic.IList<T> Listar(string propiedad, string objetivo)
        {            
            ISession session = DL_NHibernate.GetSession().OpenSession();
            IList<T> lista;
            if (propiedad == null)
            {
                lista = session.CreateCriteria(typeof(T)).List<T>();
            }
            else
            {
                lista = session.CreateCriteria(typeof(T)).Add(Restrictions.Like(propiedad, objetivo +"%")).List<T>();
            }
            
            return lista;
        }


        public IList<T> Listar(string relacion, string alias, string propiedad, string objetivo)
        {
            ISession session = DL_NHibernate.GetSession().OpenSession();
            IList<T> lista;
            //DateTime.Parse(objetivo)
            lista = session.CreateCriteria(typeof(T)).CreateCriteria(relacion).Add(Restrictions.Eq(propiedad, objetivo)).List<T>();
            return lista;
        }


        public IList<T> ListarXQuery(string query)
        {
            ISession session = DL_NHibernate.GetSession().OpenSession();
            var lista = session.CreateQuery(query).SetResultTransformer(new DistinctRootEntityResultTransformer()).List<T>();
            return lista;
        }
    }
}
